package com.rsy.crm.settings.web.controller;


import com.rsy.crm.settings.domain.User;
import com.rsy.crm.settings.service.Impl.UserServiceImpl;
import com.rsy.crm.settings.service.UserService;
import com.rsy.crm.util.MD5Util;
import com.rsy.crm.util.PrintJson;
import com.rsy.crm.util.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 */
public class UserControllerServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到用户控制器");

        String path = request.getServletPath();

        if("/settings/user/login.do".equals(path)){

            login(request,response);

        }else if("/settings/user/xxx.do".equals(path)){


        }

    }

    private void login(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("进入到验证登录操作");

        String loginAct = request.getParameter("loginAct");
        String loginPwd = request.getParameter("loginPwd");
        //将密码的明文形式转换为MD5的密文形式
        loginPwd = MD5Util.getMD5(loginPwd);
        //接收浏览器端的ip地址
        String ip = request.getRemoteAddr();
        System.out.println("--------------ip:"+ip);


        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());
        //UserService us = new UserServiceImpl();
        try {

            User user = us.login(loginAct,loginPwd,ip);

            request.getSession().setAttribute("user", user);

            PrintJson.printJsonFlag(response, true);

        }catch(Exception e){
            e.printStackTrace();

            String msg = e.getMessage();

            Map<String,Object> map = new HashMap<String,Object>();
            map.put("success", false);
            map.put("msg", msg);
            PrintJson.printJsonObj(response, map);


        }


    }
}




































